package net.zjitc.service.impl;

import net.zjitc.mapper.OrdersMapper;
import net.zjitc.mapper.ProductsMapper;
import net.zjitc.pojo.OrderItem;
import net.zjitc.pojo.Orders;
import net.zjitc.pojo.OrdersCondition;
import net.zjitc.pojo.Products;
import net.zjitc.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author admin
 * @Date 2019/4/9 8:19
 */
@Service
@Transactional
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    OrdersMapper ordersMapper;

    @Autowired
    ProductsMapper productsMapper;
    @Override
    public List<Orders> findAll() {
        return ordersMapper.findAll();
    }

    @Override
    public Orders findOrderById(String id) {
        Orders order = ordersMapper.findOrderById(id);
        List<OrderItem> itemList = order.getOrderItemList();
        for(OrderItem item:itemList){
            Products product = productsMapper.findProductById(item.getProduct_id());
            item.setProducts(product);
        }
        return order;
    }

    @Override
    public List<Orders> searchByOrdersCondition(OrdersCondition ordersCondition) {
        return ordersMapper.searchByOrdersCondition(ordersCondition);
    }

    @Override
    public List<OrderItem> findNewThreeProducts() {
        List<Orders> productsId = ordersMapper.findNewThreeProductsId();
        List<OrderItem> orderItemList = ordersMapper.findItemByOrdersid(productsId);
        return orderItemList;
    }
}
